package com.thti.mecool.client.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.ItemVolume;
import com.thti.mecool.client.base.SaleVolumeData;
import com.thti.mecool.client.widget.Toolbar;

public class SaleVolumeActivity extends BaseActivity {
	private ListView saleList;
	SaleVolumeData saleVolumeData;
	ArrayList<ItemVolume> VolumeList;
	ImageButton saleUploadButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/**
		 * 测试数据
		 */
		VolumeList = new ArrayList<ItemVolume>();
		for (int i = 1; i <= 3; i++) {
			ItemVolume iv = new ItemVolume();
			iv.setEditTime("12/05/03 13:21");
			iv.setItemState(i);
			if(i==1){
				iv.setStoreName("家乐福店销-家乐福武宁店");
				iv.setUploadState(true);
				ArrayList<String> itemNameList=new ArrayList<String>();
				itemNameList.add("天喔茶庄柚子茶1");
				itemNameList.add("天喔茶庄柚子茶2");
				itemNameList.add("天喔茶庄柚子茶3");
				itemNameList.add("天喔茶庄柚子茶4");
				itemNameList.add("天喔茶庄柚子茶1");
				itemNameList.add("天喔茶庄柚子茶2");
				itemNameList.add("天喔茶庄柚子茶3");
				itemNameList.add("天喔茶庄柚子茶4");
				iv.setItemNameList(itemNameList);
			}else if(i==2){
				iv.setStoreName("家乐福店销-家乐福武宁店");
				iv.setUploadState(false);
				ArrayList<String> itemNameList=new ArrayList<String>();
				itemNameList.add("11-PCC Body Wash");
				itemNameList.add("11-PCC Body Wash");
				itemNameList.add("11-PCC Body Wash");
				itemNameList.add("11-PCC Body Wash");
				iv.setItemNameList(itemNameList);
			}else if(i==3){
				iv.setStoreName("家乐福店销-家乐福武宁店");
				iv.setUploadState(false);
				ArrayList<String> itemNameList=new ArrayList<String>();
				itemNameList.add("联合利华");
				itemNameList.add("保洁");
				itemNameList.add("联合利华");
				itemNameList.add("联合利华");
				iv.setItemNameList(itemNameList);
			}

			VolumeList.add(iv);
		}
		SaleVolumeData svd = new SaleVolumeData();
		svd.setVolumelist(VolumeList);
		setData(svd);
		/**
	 * 
	 */

		noTitle();
		setContentView(R.layout.mecool_sale_volume);
		initToolbar();
		initData();
		VolumeList = saleVolumeData.getVolumelist();
		saleUploadButton=(ImageButton)findViewById(R.id.mecool_sale_volume_upload);
		saleUploadButton.setOnClickListener(clickListener);
		saleList = (ListView) findViewById(R.id.mecool_sale_volume_list);
		if(VolumeList!=null){
			saleList.setAdapter(SaleListAdapter);
			saleList.setOnItemClickListener(saleVolumeItemListener);}
	}
	
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("销量管理");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
	}};

	private void initData() {
		if (ContainsDataList(SaleVolumeData.class)) {
			saleVolumeData = (SaleVolumeData) getData(SaleVolumeData.class);
		} else {
			saleVolumeData = new SaleVolumeData();
		}
	}
	
	private OnItemClickListener saleVolumeItemListener=new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent i=new Intent();
			switch (VolumeList.get(arg2).getItemState()) {
			case 1:
				i.setClass(SaleVolumeActivity.this, SaleSKUActivity.class);
				i.putExtra("ItemNameList", VolumeList.get(arg2).getItemNameList());
				i.putExtra("titlename",VolumeList.get(arg2).getStoreName());
				i.putExtra("uploadtitle", "sku上报");
				startActivity(i);
				break;
			case 2:
				i.setClass(SaleVolumeActivity.this, SaleBrandAndCategoryActivity.class);
				i.putExtra("NameList", VolumeList.get(arg2).getItemNameList());
				i.putExtra("titlename",VolumeList.get(arg2).getStoreName());
				i.putExtra("uploadtitle", "品类上报");
				startActivity(i);
				break;
			case 3:
				i.setClass(SaleVolumeActivity.this, SaleBrandAndCategoryActivity.class);
				i.putExtra("NameList", VolumeList.get(arg2).getItemNameList());
				i.putExtra("titlename",VolumeList.get(arg2).getStoreName());
				i.putExtra("uploadtitle", "品牌上报");
				startActivity(i);
				break;
			}
		}
	};

	OnClickListener clickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent(SaleVolumeActivity.this,SaleUploadActivity.class);
			startActivity(intent);
		}
	};
	
	private BaseAdapter SaleListAdapter = new BaseAdapter() {

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			if (arg1 == null) {
				arg1 = getLayoutInflater().inflate(
						R.layout.mecool_sale_volume_listitem, null);
			}
			TextView uploadState=(TextView)arg1.findViewById(R.id.mecool_sale_item_upload_state);
			ImageView state = (ImageView) arg1
					.findViewById(R.id.mecool_sale_volume_item_store_state);
			TextView store = (TextView) arg1
					.findViewById(R.id.mecool_sale_volume_item_store_text);
			TextView time = (TextView) arg1
					.findViewById(R.id.mecool_sale_volume_item_time);
			store.setText(VolumeList.get(arg0).getStoreName());
			time.setText(VolumeList.get(arg0).getEditTime());
			if(VolumeList.get(arg0).isUploadState()){
				uploadState.setVisibility(0);
			}
			switch (VolumeList.get(arg0).getItemState()) {
			case 1:
				state.setImageResource(R.drawable.mecool_sale_volume_sku);
				break;
			case 2:
				state.setImageResource(R.drawable.mecool_sale_volume_kind);
				break;
			case 3:
				state.setImageResource(R.drawable.mecool_sale_volume_brand);
				break;

			}
			return arg1;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public Object getItem(int arg0) {
			return VolumeList.get(arg0).getStoreName();
		}

		@Override
		public int getCount() {
			return VolumeList.size();
		}
	};
	
	
	
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		if(intent.getBooleanExtra("isadd", false)){
			ItemVolume itemVolume = new ItemVolume();
			itemVolume.setStoreName(intent.getStringExtra("name"));
			itemVolume.setEditTime(intent.getStringExtra("time"));
			itemVolume.setUploadState(false);
			ArrayList<String> itemNameList=new ArrayList<String>();
			itemNameList.add("三聚氰胺");
			itemNameList.add("猛牛");
			itemNameList.add("地沟油");
			itemVolume.setItemNameList(itemNameList);
			VolumeList.add(itemVolume);
			SaleListAdapter.notifyDataSetChanged();
		}
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	
	
}
